Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
40 lines · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { APIRoute } from "astro";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { and, eq } from "drizzle-orm";
import { getDatabase, schema } from "@/db";
import { forbidden, notFound, success, unauthorized } from "@/lib/api";
import { withErrorHandler } from "@/lib/errors";
import { getUserFromRequest } from "@/lib/auth";
import { getDashboard } from "@/lib/analytics-advanced";

export const GET: APIRoute = withErrorHandler(async ({ params, request }) => {
  const id = params.id;
  if (!id) return notFound("Dashboard not found");

  const user = await getUserFromRequest(request);
  const payload = await getDashboard(id);
  if (!payload) return notFound("Dashboard not found");

  const canRead = payload.dashboard.isPublic || user?.userId === payload.dashboard.userId || user?.isAdmin;
  if (!canRead) return notFound("Dashboard not found");
  return success(payload);
});

export const DELETE: APIRoute = withErrorHandler(async ({ params, request }) => {
  const id = params.id;
  if (!id) return notFound("Dashboard not found");

  const user = await getUserFromRequest(request);
  if (!user) return unauthorized();

  const db = getDatabase() as NodePgDatabase<typeof schema>;
  const dashboard = await db.query.customDashboards?.findFirst({
    where: eq(schema.customDashboards.id, id),
  });
  if (!dashboard) return notFound("Dashboard not found");
  if (dashboard.userId !== user.userId && !user.isAdmin) return forbidden();

  await db.delete(schema.customDashboards).where(eq(schema.customDashboards.id, id));
  return success({ deleted: true });
});